package com.xinhua.geng.service;


import com.xinhua.geng.pojo.*;
import com.xinhua.geng.dao.UserMapper;
import com.xinhua.geng.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class UserServiceImpl  implements  UserService{


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Object> selectName() {
        List<Object> names = userMapper.selectName();
        for(Object object : names) {
            redisUtil.lSet("name",object);
        }

        return names;
    }

    @Override
    public Map fingUser(String username, String password) {
        Map map = new HashMap<>();
        String result = userMapper.fingUser(username,password);
        if (result == null || result.length() == 0){
            String result1 = userMapper.findTeacher(username,password);
            if (result1 == null || result1.length() == 0){

                return  null;
            }else {
                map.put("shen","teacher");
                return map;
            }
        }
        map.put("shen","student");
        map.put("id",result);
        return  map;

    }

    @Override
    public String findTeacher(String name, String password) {
        return userMapper.findTeacher(name,password);
    }

    @Override
    public List<User> findClass(String name) {
        List<User> users = userMapper.findClass(name);
        for(User user : users) {
            redisUtil.lSet("find_class"+name,user);
        }
        return users;
    }

    @Override
    public User getUser(int id) {
        User user = userMapper.getUser(id);
        redisUtil.set("get_user"+id,user);
        return  user;
    }

    @Override
    public List<totalOne> classCount() {
        List<totalOne> totalOnes = userMapper.classCount();
        for (totalOne t: totalOnes) {
            redisUtil.lSet("class_count",t);
        }
        return totalOnes;
    }

    @Override
    public List<TotalTwo> ageCount() {
        List<TotalTwo> totalTwos = userMapper.ageCount();
        for(TotalTwo t : totalTwos){
            redisUtil.lSet("age_count",t);
        }
        return userMapper.ageCount();
    }

    @Override
    public List<User> findUserByName(String name) {
        List<User> users = userMapper.findUserByName(name);
        for (User user : users) {
            redisUtil.lSet("findu_name"+name,user);
        }
        return users;
    }

    @Override
    public List<kao> getKao() {
        List<kao> kaos = userMapper.getKao();
        for (kao k : kaos){
            redisUtil.lSet("kao",k);
        }
        return kaos;
    }
    @Override
    public List<Score> getScore(int id) {
        List<Score> scores = userMapper.getScore(id);
        for (Score score : scores) {
            redisUtil.lSet("get_score"+id,score);
        }
        return userMapper.getScore(id);
    }

    @Override
    public boolean deleteStu(int id) {

        boolean c = userMapper.deleteScore(id);
        boolean b = userMapper.deleteStu(id);
        if (c && b) {
            redisUtil.flushAll();
            return true;
        }else{
            return false;
        }

    }

    @Override
    public boolean setUser(int id, String name, int age, String sex, String classNo, String ji) {
        userMapper.setUser(id, name, age, sex, classNo, ji);

        return  redisUtil.flushAll();
    }

    @Override
    public boolean addUser(String name, int age, String sex, String classNo, String ji) {
        userMapper.addUser(name, age, sex, classNo, ji);

        return redisUtil.flushAll();
    }



    @Override
    public boolean deleteScore(int id) {
        return userMapper.deleteScore(id);
    }

    @Override
    public boolean insertScore(int id) {
        return userMapper.insertScore(id);
    }

    @Override
    public int selectMax() {
        return userMapper.selectMax();
    }


}
